{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import cv2\n",
    "import tensorflow as tf\n",
    "\n",
    "from skimage.measure import compare_ssim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def mean_square_error(first_image, second_image):\n",
    "    # the 'Mean Squared Error' between the two images is the\n",
    "    # sum of the squared difference between the two images;\n",
    "    error = np.sum((first_image.astype(\"float\") - second_image.astype(\"float\")) ** 2)\n",
    "    error /= float(first_image.shape[0] * first_image.shape[1])\n",
    "    \n",
    "    # return the MSE, the lower the error, the more \"similar\"\n",
    "    # the two images are\n",
    "    return error\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# load all images that needs comparing \n",
    "original = cv2.imread(\"image_compare_folder/original_IM-0001-0019.jpg\")\n",
    "cheapScale_factor_2 = cv2.imread(\"image_compare_folder/cheapScale_factor_2_IM-0001-0019.jpg\")\n",
    "cheapScale_factor_3 = cv2.imread(\"image_compare_folder/cheapScale_factor_3_IM-0001-0019.jpg\")\n",
    "cheapScale_factor_4 = cv2.imread(\"image_compare_folder/cheapScale_factor_4_IM-0001-0019.jpg\")\n",
    "\n",
    "RAISR_factor_2 = cv2.imread(\"image_compare_folder/RAISR_factor_2_IM-0001-0019.jpg\")\n",
    "RAISR_factor_3 = cv2.imread(\"image_compare_folder/RAISR_factor_3_IM-0001-0019.jpg\")\n",
    "RAISR_factor_4 = cv2.imread(\"image_compare_folder/RAISR_factor_4_IM-0001-0019.jpg\")\n",
    "\n",
    "SRCNN_factor_2 = cv2.imread(\"image_compare_folder/SRCNN_factor_2_IM-0001-0019.jpg\")\n",
    "SRCNN_factor_3 = cv2.imread(\"image_compare_folder/SRCNN_factor_3_IM-0001-0019.jpg\")\n",
    "SRCNN_factor_4 = cv2.imread(\"image_compare_folder/SRCNN_factor_4_IM-0001-0019.jpg\")\n",
    " \n",
    "# convert images to greyScale\n",
    "original = cv2.cvtColor(original, cv2.COLOR_BGR2GRAY)\n",
    "cheapScale_factor_2 = cv2.cvtColor(cheapScale_factor_2, cv2.COLOR_BGR2GRAY)\n",
    "cheapScale_factor_3 = cv2.cvtColor(cheapScale_factor_3, cv2.COLOR_BGR2GRAY)\n",
    "cheapScale_factor_4 = cv2.cvtColor(cheapScale_factor_4, cv2.COLOR_BGR2GRAY)\n",
    "RAISR_factor_2 = cv2.cvtColor(RAISR_factor_2, cv2.COLOR_BGR2GRAY)\n",
    "RAISR_factor_3 = cv2.cvtColor(RAISR_factor_3, cv2.COLOR_BGR2GRAY)\n",
    "RAISR_factor_4 = cv2.cvtColor(RAISR_factor_4, cv2.COLOR_BGR2GRAY)\n",
    "\n",
    "SRCNN_factor_2 = cv2.cvtColor(SRCNN_factor_2, cv2.COLOR_BGR2GRAY)\n",
    "SRCNN_factor_3 = cv2.cvtColor(SRCNN_factor_3, cv2.COLOR_BGR2GRAY)\n",
    "SRCNN_factor_4 = cv2.cvtColor(SRCNN_factor_4, cv2.COLOR_BGR2GRAY)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mse_cheapScale_factor_2 = mean_square_error(cheapScale_factor_2, original)\n",
    "mse_cheapScale_factor_3 = mean_square_error(cheapScale_factor_3, original)\n",
    "mse_cheapScale_factor_4 = mean_square_error(cheapScale_factor_4, original)\n",
    "\n",
    "mse_RAISR_factor_2 = mean_square_error(RAISR_factor_2, original)\n",
    "mse_RAISR_factor_3 = mean_square_error(RAISR_factor_3, original)\n",
    "mse_RAISR_factor_4 = mean_square_error(RAISR_factor_4, original)\n",
    "\n",
    "mse_SRCNN_factor_2 = mean_square_error(SRCNN_factor_2, original)\n",
    "mse_SRCNN_factor_3 = mean_square_error(SRCNN_factor_3, original)\n",
    "mse_SRCNN_factor_4 = mean_square_error(SRCNN_factor_4, original)\n",
    "\n",
    "print(\"mse value: \")\n",
    "print(\"    \", \"CheapScale_factor_2: \", mse_cheapScale_factor_2, \" CheapScale_factor_3: \", mse_cheapScale_factor_3, \" CheapScale_factor_4: \", mse_cheapScale_factor_4)\n",
    "print(\"    \", \"RAISR_factor_2: \", mse_RAISR_factor_2, \" RAISR_factor_3: \", mse_RAISR_factor_3, \" RAISR_factor_4: \", mse_RAISR_factor_4)\n",
    "print(\"    \", \"SRCNN_factor_2: \", mse_SRCNN_factor_2, \" SRCNN_factor_3: \", mse_SRCNN_factor_3, \" SRCNN_factor_4: \", mse_SRCNN_factor_4)\n",
    "\n",
    "\n",
    "# ssim_cheapScale_factor_2 = compare_ssim(cheapScale_factor_2, original)\n",
    "# ssim_cheapScale_factor_3 = compare_ssim(cheapScale_factor_3, original)\n",
    "# ssim_RAISR_factor_2 = compare_ssim(RAISR_factor_2, original)\n",
    "# ssim_RAISR_factor_3 = compare_ssim(RAISR_factor_3, original)\n",
    "\n",
    "# print(\"SSIM value: \")\n",
    "# print(\"    \", \"CheapScale_factor_2: \", ssim_cheapScale_factor_2, \" CheapScale_factor_3: \", ssim_cheapScale_factor_3)\n",
    "# print(\"    \", \"RAISR_factor_2: \", ssim_RAISR_factor_2, \" RAISR_factor_3: \", ssim_RAISR_factor_3)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"-------------tensorflow module method compare-----------------\")\n",
    "sess = tf.Session()\n",
    "\n",
    "original = tf.read_file(\"image_compare_folder/original_IM-0001-0019.jpg\")\n",
    "\n",
    "cheapScale_factor_2 = tf.read_file(\"image_compare_folder/cheapScale_factor_2_IM-0001-0019.jpg\")\n",
    "cheapScale_factor_3 = tf.read_file(\"image_compare_folder/cheapScale_factor_3_IM-0001-0019.jpg\")\n",
    "cheapScale_factor_4 = tf.read_file(\"image_compare_folder/cheapScale_factor_4_IM-0001-0019.jpg\")\n",
    "\n",
    "RAISR_factor_2 = tf.read_file(\"image_compare_folder/RAISR_factor_2_IM-0001-0019.jpg\")\n",
    "RAISR_factor_3 = tf.read_file(\"image_compare_folder/RAISR_factor_3_IM-0001-0019.jpg\")\n",
    "RAISR_factor_4 = tf.read_file(\"image_compare_folder/RAISR_factor_4_IM-0001-0019.jpg\")\n",
    "\n",
    "SRCNN_factor_2 = tf.read_file(\"image_compare_folder/SRCNN_factor_2_IM-0001-0019.jpg\")\n",
    "SRCNN_factor_3 = tf.read_file(\"image_compare_folder/SRCNN_factor_3_IM-0001-0019.jpg\")\n",
    "SRCNN_factor_4 = tf.read_file(\"image_compare_folder/SRCNN_factor_4_IM-0001-0019.jpg\")\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "original = tf.image.decode_jpeg(original, channels =1)\n",
    "\n",
    "cheapScale_factor_2 = tf.image.decode_jpeg(cheapScale_factor_2, channels = 1)\n",
    "cheapScale_factor_3 = tf.image.decode_jpeg(cheapScale_factor_3, channels = 1)\n",
    "cheapScale_factor_4 = tf.image.decode_jpeg(cheapScale_factor_4, channels = 1)\n",
    "\n",
    "RAISR_factor_2 = tf.image.decode_jpeg(RAISR_factor_2, channels = 1)\n",
    "RAISR_factor_3 = tf.image.decode_jpeg(RAISR_factor_3, channels = 1)\n",
    "RAISR_factor_4 = tf.image.decode_jpeg(RAISR_factor_4, channels = 1)\n",
    "\n",
    "SRCNN_factor_2 = tf.image.decode_jpeg(SRCNN_factor_2, channels = 1)\n",
    "SRCNN_factor_3 = tf.image.decode_jpeg(SRCNN_factor_3, channels = 1)\n",
    "SRCNN_factor_4 = tf.image.decode_jpeg(SRCNN_factor_4, channels = 1)\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "original = tf.image.convert_image_dtype(original, tf.float32)\n",
    "\n",
    "cheapScale_factor_2 = tf.image.convert_image_dtype(cheapScale_factor_2, tf.float32)\n",
    "cheapScale_factor_3 = tf.image.convert_image_dtype(cheapScale_factor_3, tf.float32)\n",
    "cheapScale_factor_4 = tf.image.convert_image_dtype(cheapScale_factor_4, tf.float32)\n",
    "\n",
    "RAISR_factor_2 = tf.image.convert_image_dtype(RAISR_factor_2, tf.float32)\n",
    "RAISR_factor_3 = tf.image.convert_image_dtype(RAISR_factor_3, tf.float32)\n",
    "RAISR_factor_4 = tf.image.convert_image_dtype(RAISR_factor_4, tf.float32)\n",
    "\n",
    "SRCNN_factor_2 = tf.image.convert_image_dtype(SRCNN_factor_2, tf.float32)\n",
    "SRCNN_factor_3 = tf.image.convert_image_dtype(SRCNN_factor_3, tf.float32)\n",
    "SRCNN_factor_4 = tf.image.convert_image_dtype(SRCNN_factor_4, tf.float32)\n",
    "\n",
    "# ssim_factor = tf.image.ssim(cheapScale_factor_2, original, max_val=1.0)\n",
    "# value = sess.run(ssim_factor)\n",
    "# print(\"1\")\n",
    "# print(value)\n",
    "\n",
    "\n",
    "ssim_cheapScale_factor_2 = sess.run(tf.image.ssim(cheapScale_factor_2, original, max_val=1.0))\n",
    "ssim_cheapScale_factor_3 = sess.run(tf.image.ssim(cheapScale_factor_3, original, max_val=1.0))\n",
    "ssim_cheapScale_factor_4 = sess.run(tf.image.ssim(cheapScale_factor_4, original, max_val=1.0))\n",
    "\n",
    "ssim_RAISR_factor_2 = sess.run(tf.image.ssim(RAISR_factor_2, original, max_val=1.0))\n",
    "ssim_RAISR_factor_3 = sess.run(tf.image.ssim(RAISR_factor_3, original, max_val=1.0))\n",
    "ssim_RAISR_factor_4 = sess.run(tf.image.ssim(RAISR_factor_4, original, max_val=1.0))\n",
    "\n",
    "ssim_SRCNN_factor_2 = sess.run(tf.image.ssim(SRCNN_factor_2, original, max_val=1.0))\n",
    "ssim_SRCNN_factor_3 = sess.run(tf.image.ssim(SRCNN_factor_3, original, max_val=1.0))\n",
    "ssim_SRCNN_factor_4 = sess.run(tf.image.ssim(SRCNN_factor_4, original, max_val=1.0))\n",
    "\n",
    "print(\"SSIM value: \")\n",
    "print(\"    \", \"CheapScale_factor_2: \", ssim_cheapScale_factor_2, \" CheapScale_factor_3: \", ssim_cheapScale_factor_3, \" CheapScale_factor_3\", ssim_cheapScale_factor_4)\n",
    "print(\"    \", \"RAISR_factor_2: \", ssim_RAISR_factor_2, \" RAISR_factor_3: \", ssim_RAISR_factor_3, \" RAISR_factor_4: \", ssim_RAISR_factor_4 )\n",
    "print(\"    \", \"SRCNN_factor_2: \", ssim_SRCNN_factor_2, \" SRCNN_factor_3: \", ssim_SRCNN_factor_3, \" SRCNN_factor_4: \", ssim_SRCNN_factor_4)\n",
    "\n",
    "\n",
    "\n",
    "psnr_cheapScale_factor_2 = sess.run(tf.image.psnr(cheapScale_factor_2, original, max_val=1.0))\n",
    "psnr_cheapScale_factor_3 = sess.run(tf.image.psnr(cheapScale_factor_3, original, max_val=1.0))\n",
    "psnr_cheapScale_factor_4 = sess.run(tf.image.psnr(cheapScale_factor_4, original, max_val=1.0))\n",
    "\n",
    "psnr_RAISR_factor_2 = sess.run(tf.image.psnr(RAISR_factor_2, original, max_val=1.0))\n",
    "psnr_RAISR_factor_3 = sess.run(tf.image.psnr(RAISR_factor_3, original, max_val=1.0))\n",
    "psnr_RAISR_factor_4 = sess.run(tf.image.psnr(RAISR_factor_4, original, max_val=1.0))\n",
    "\n",
    "psnr_SRCNN_factor_2 = sess.run(tf.image.psnr(SRCNN_factor_2, original, max_val=1.0))\n",
    "psnr_SRCNN_factor_3 = sess.run(tf.image.psnr(SRCNN_factor_3, original, max_val=1.0))\n",
    "psnr_SRCNN_factor_4 = sess.run(tf.image.psnr(SRCNN_factor_4, original, max_val=1.0))\n",
    "\n",
    "\n",
    "print(\"PSNR value: \")\n",
    "print(\"    \", \"CheapScale_factor_2: \", psnr_cheapScale_factor_2, \" CheapScale_factor_3: \", psnr_cheapScale_factor_3, \" CheapScale_factor_4: \", psnr_cheapScale_factor_4 )\n",
    "print(\"    \", \"RAISR_factor_2: \", psnr_RAISR_factor_2, \" RAISR_factor_3: \", psnr_RAISR_factor_3, \" RAISR_factor_4: \", psnr_RAISR_factor_4 )\n",
    "print(\"    \", \"SRCNN_factor_2: \", psnr_SRCNN_factor_2, \" SRCNN_factor_3: \", psnr_SRCNN_factor_3, \" SRCNN_factor_4: \", psnr_SRCNN_factor_4)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
